﻿// Copyright: 2012 
// Author: Minh Vu - YoungJ
// File name: RssReader.cs
// Solution: WindowsPhoneRssReader
// Project: WindowsPhoneRssReader
// Time: 1:03 PM 07/05/2012

using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using WindowsPhoneRssReader.Utils;

namespace WindowsPhoneRssReader.Models
{
    public class RssReader : MyBase
    {
        public void LoadData()
        {
            IsDataLoaded = true;
            var list = XmlAction.ReadFeedsData();
            if (list == null) return;
            var feeds = new ObservableCollection<Feed>();
            foreach (var feed in list)
            {
                feed.ParentModel = this;
                feeds.Add(feed);
            }
            Feeds = feeds;
            Feeds.CollectionChanged += (sender, e) => UpdateLatestItems();
        }

        public void UpdateLatestItems()
        {
            //var latest = new ObservableCollection<FeedItem>();
            //foreach (var item in Feeds.SelectMany(f => f.FeedItems).OrderByDescending(i => i.DatePublished).Take(30))
            //{
            //    latest.Add(item);
            //}
            LatestItems.Clear();
            LatestItems.AddRanged(Feeds.SelectMany(f => f.FeedItems).OrderByDescending(i => i.DatePublished).Take(30));

            UpdateAllItems();
            UpdateUnreadItem();
        }

        public void UpdateAllItems()
        {
            AllItems.Clear();
            AllItems.AddRanged(Feeds.SelectMany(f => f.FeedItems));
        }

        public void UpdateUnreadItem()
        {
            UnreadItems.Clear();
            UnreadItems.AddRanged(Feeds.SelectMany(f => f.FeedItems).Where(item => !item.Read));
        }

        public void UpdateFavourites()
        {
            //var favs = new ObservableCollection<FeedItem>();
            //foreach (var item in Feeds.SelectMany(f => f.FeedItems).Where(i => i.Favourite))
            //{
            //    favs.Add(item);
            //}
            FavouriteItems.Clear();
            FavouriteItems.AddRanged(Feeds.SelectMany(f => f.FeedItems).Where(i => i.Favourite));
        }
        private AddRangerObservableCollection<FeedItem> _latestItems = new AddRangerObservableCollection<FeedItem>();
        public AddRangerObservableCollection<FeedItem> LatestItems
        {
            get { return _latestItems; }
            set
            {
                _latestItems = value;
                NotifyPropertyChanged("LatestItems");
            }
        }

        private AddRangerObservableCollection<FeedItem> _favouriteItems = new AddRangerObservableCollection<FeedItem>();
        public AddRangerObservableCollection<FeedItem> FavouriteItems
        {
            get { return _favouriteItems; }
            set
            {
                _favouriteItems = value;
                NotifyPropertyChanged("FavouriteItems");
            }
        }

        private AddRangerObservableCollection<FeedItem> _unreadItems = new AddRangerObservableCollection<FeedItem>();
        public AddRangerObservableCollection<FeedItem> UnreadItems
        {
            get { return _unreadItems; }
            set
            {
                _unreadItems = value;
                NotifyPropertyChanged("UnreadItems");
            }
        }


        private AddRangerObservableCollection<FeedItem> _allItems = new AddRangerObservableCollection<FeedItem>();
        public AddRangerObservableCollection<FeedItem> AllItems
        {
            get
            {
                return _allItems;
            }
            set
            {
                _allItems = value;
                NotifyPropertyChanged("AllItems");
            }
        }

        private ObservableCollection<Feed> _feeds = new ObservableCollection<Feed>();
        public ObservableCollection<Feed> Feeds
        {
            get { return _feeds; }
            set { _feeds = value; NotifyPropertyChanged("Feeds"); }
        }



        public bool IsDataLoaded { get; set; }


    }
}